package com.zzyl.serve.controller;

import java.time.LocalDateTime;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.common.core.domain.R;
import com.zzyl.serve.dto.NursingTaskCancelDto;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.framework.interceptor.MyMetaObjectHandler;
import com.zzyl.serve.dto.NursingTaskDto;
import com.zzyl.serve.dto.NursingTaskDto;
import com.zzyl.serve.vo.NursingTaskDetailVo;
import com.zzyl.serve.vo.NursingTaskVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.serve.domain.NursingTask;
import com.zzyl.serve.service.INursingTaskService;
import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.common.core.page.TableDataInfo;

/**
 * 护理任务Controller
 * 
 * @author liuyp
 * @date 2025-07-30
 */
@RestController
@RequestMapping("/nursing/task")
@Api(tags = "护理任务管理")
public class NursingTaskController extends BaseController
{
    @Autowired
    private INursingTaskService nursingTaskService;

    @Autowired
    private MyMetaObjectHandler myMetaObjectHandler;

    @PutMapping("/updateTime")
    @ApiOperation("任务改期")
    public AjaxResult updateTime(@RequestBody NursingTaskDto dto) {

        LambdaUpdateWrapper<NursingTask> wrapper = Wrappers.<NursingTask>lambdaUpdate().eq(NursingTask::getId, dto.getTaskId())
                .set(NursingTask::getEstimatedServerTime, dto.getEstimatedServerTime());
        boolean update = nursingTaskService.update(wrapper);
        if (!update) {
            throw  new BaseException("任务改期失败");
        }
        return AjaxResult.success();
    }


    @PutMapping("/do")
    @ApiOperation("执行任务")
    public AjaxResult executeTask(@RequestBody NursingTaskDto dto) {

        LambdaUpdateWrapper<NursingTask> wrapper = Wrappers.<NursingTask>lambdaUpdate().eq(NursingTask::getId, dto.getTaskId())
                .set(NursingTask::getRealServerTime, dto.getEstimatedServerTime())
                .set(NursingTask::getMark, dto.getMark())
                .set(NursingTask::getTaskImage, dto.getTaskImage())
                .set(NursingTask::getStatus, 2)
                .set(NursingTask::getUpdateTime, LocalDateTime.now())
                .set(NursingTask::getUpdateBy, String.valueOf(myMetaObjectHandler.getLoginUserId()));
        boolean update = nursingTaskService.update(wrapper);
        if (!update) {
            throw new BaseException("执行任务失败");
        }
        return AjaxResult.success();
    }

    @PutMapping("/cancel")
    @ApiOperation(value = "取消任务")
    public AjaxResult cancel(@RequestBody NursingTaskCancelDto nursingTaskCancelDto){
        //修改cancel_reason字段中的内容
        nursingTaskService.cancel(nursingTaskCancelDto);
        return AjaxResult.success();
    }


    /**
     * 查询护理任务列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "获取护理任务列表")
    public TableDataInfo<List<NursingTaskVo>> list(@ApiParam(value = "护理任务查询条件") NursingTaskDto nursingTaskDto)
    {
        startPage();
        List<NursingTaskVo> list = nursingTaskService.selectNursingTaskList(nursingTaskDto);
        return getDataTable(list);
    }

//    /**
//     * 导出护理任务列表
//     */
//    @Log(title = "护理任务", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    @ApiOperation("导出护理任务列表")
//    public void export(HttpServletResponse response, @ApiParam(value = "护理任务查询条件") NursingTask nursingTask)
//    {
//        List<NursingTask> list = nursingTaskService.selectNursingTaskList(nursingTask);
//        ExcelUtil<NursingTask> util = new ExcelUtil<NursingTask>(NursingTask.class);
//        util.exportExcel(response, list, "护理任务数据");
//    }

    /**
     * 获取护理任务详细信息
     */
    @GetMapping(value = "/{id}")
    @ApiOperation("获取护理任务详细信息")
    public R<NursingTaskDetailVo> getInfo(@ApiParam(value = "护理任务ID", required = true) @PathVariable("id") Integer id)
    {
        return R.ok(nursingTaskService.selectNursingTaskById(id));
    }

    /**
     * 新增护理任务
     */
    @Log(title = "护理任务", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增护理任务")
    public AjaxResult add(@ApiParam(value = "护理任务实体", required = true) @RequestBody NursingTask nursingTask)
    {
        return toAjax(nursingTaskService.insertNursingTask(nursingTask));
    }

    /**
     * 修改护理任务
     */
    @Log(title = "护理任务", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改护理任务")
    public AjaxResult edit(@ApiParam(value = "护理任务实体", required = true) @RequestBody NursingTask nursingTask)
    {
        return toAjax(nursingTaskService.updateNursingTask(nursingTask));
    }

    /**
     * 删除护理任务
     */
    @Log(title = "护理任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除护理任务")
    public AjaxResult remove(@ApiParam(value = "护理任务ID数组", required = true) @PathVariable Long[] ids)
    {
        return toAjax(nursingTaskService.deleteNursingTaskByIds(ids));
    }
}
